<style lang="less">
    @import './login.less';
</style>

<template>
    <div class="login" @keydown.enter="handleSubmit">
        <div class="login-con">
            <Form ref="loginForm" :model="form" :rules="rules" style="padding: 26px;background-color:rgba(64,64,64,0.1);">
                <FormItem>
                    <span style="color:#2b85e4;font-family:Arial,Verdana,Sans-serif;font-weight:700;font-size:15px;">
                        <Icon type="log-in"></Icon>
                        管理员登陆 | ADMIN LOGIN
                    </span>
                </FormItem>
                <FormItem prop="userName">
                    <Input v-model="form.userName" placeholder="请输入用户名" style="color:black">
                        <span slot="prepend">
                            <Icon :size="16" type="person"></Icon>
                        </span>
                    </Input>
                </FormItem>
                <FormItem prop="password">
                    <Input type="password" v-model="form.password" placeholder="请输入密码">
                        <span slot="prepend">
                            <Icon :size="14" type="locked"></Icon>
                        </span>
                    </Input>
                </FormItem>
                <FormItem>
                    <Row>
                        <Col span="18">
                            <Input v-model="form.validcode" placeholder="请输入验证码"></Input>
                        </Col>
                        <Col span="5">
                            <img :src="image" alt="验证码" height="30" style="cursor: pointer;" title="看不清可单击图片刷新" @click="getValidateCode"></img>
                        </Col>
                    </Row>
                </FormItem>
                <FormItem>
                    <Button @click="handleSubmit" type="primary" long>登录</Button>
                </FormItem>
            </Form>
        </div>
    </div>
</template>

<script>
import Cookies from 'js-cookie';

export default {
    data () {
        return {
            image:'',
            form: {
                userName: '',
                password: '',
                validcode: '',
                remember: true
            },
            rules: {
                userName: [
                    { required: true, message: '账号不能为空', trigger: 'blur' }
                ],
                password: [
                    { required: true, message: '密码不能为空', trigger: 'blur' }
                ],
                validcode: [
                    { required: true, message: '验证码不能为空', trigger: 'blur' }
                ]
            }
        };
    },
    methods: {
        getValidateCode(){
            this.api.post("member/validate_code",{},res=>{
                this.image=res.value;
            })
        },
        handleSubmit () {
            this.$refs.loginForm.validate((valid) => {
                if (valid) {
                    this.api.post("admin/login", {username: this.form.userName, password: this.form.password,validate_code: this.form.validcode }, res => {
                        if(!res.value) { this.$CustomMessage.error(res.message); this.getValidateCode(); return; }
                        if(this.form.remember){
                            Cookies.set('membername',this.form.userName);
                            Cookies.set('memberpass',this.form.password);
                        }else{
                            Cookies.remove('membername');
                            Cookies.remove('memberpass');
                        }
                        this.$store.commit('login', {
                            username: this.form.userName,
                            password: this.form.password,
                            access: 'admin',
                            token: res.message
                        });
                        this.$router.push({
                            name: 'home_index'
                        });
                    });
                }
            });
        }
    },
    created(){
        this.form.userName=Cookies.get('membername');
        this.form.password=Cookies.get('memberpass');
        this.api.post("member/validate_code",{},res=>{
            this.image=res.value;
        }) 
    }
};
</script>

<style>

</style>
